package cn.edu.tju.elm.controller;

import cn.edu.tju.core.model.HttpResult;
import cn.edu.tju.core.model.ResultCodeEnum;
import cn.edu.tju.core.security.service.UserService;
import cn.edu.tju.elm.model.DeliveryAddress;
import cn.edu.tju.elm.service.AddressService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api")
@Tag(name="管理地址", description = "对配送地址的增删改查")
public class AddressController {
    @Autowired
    private AddressService addressService;

    @Autowired
    private UserService userService;

    @PostMapping("/addresses")
    @Operation(summary = "添加配送地址", description = "添加新的配送地址")
    public HttpResult<DeliveryAddress> addDeliveryAddress(@RequestBody DeliveryAddress deliveryAddress) {
        try {
            DeliveryAddress savedAddress = addressService.addDeliveryAddress(deliveryAddress);
            return HttpResult.success(savedAddress);
        } catch (IllegalArgumentException e) {
            return HttpResult.failure(ResultCodeEnum.BAD_REQUEST, e.getMessage());
        } catch (Exception e) {
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, "添加配送地址失败: " + e.getMessage());
        }
    }

    @GetMapping("/addresses/{id}")
    @Operation(summary = "获取配送地址详情", description = "根据ID获取配送地址详情")
    public HttpResult<DeliveryAddress> getDeliveryAddressById(@PathVariable Long id) {
        Optional<DeliveryAddress> address = addressService.getDeliveryAddressById(id);
        if(address.isPresent()) {
            return HttpResult.success(address.get());
        } else {
            return HttpResult.failure(ResultCodeEnum.NOT_FOUND);
        }
    }

    @GetMapping("/addresses")
    @Operation(summary = "获取用户配送地址列表", description = "获取指定用户的所有配送地址")
    public HttpResult<List<DeliveryAddress>> getDeliveryAddressesByUserId(@RequestParam Long userId) {
        List<DeliveryAddress> addresses = addressService.getDeliveryAddressesByUserId(userId);
        return HttpResult.success(addresses);
    }

    @PutMapping("/addresses/{id}")
    @Operation(summary = "更新配送地址", description = "更新指定ID的配送地址信息")
    public HttpResult<DeliveryAddress> updateDeliveryAddress(@PathVariable Long id, @RequestBody DeliveryAddress deliveryAddress) {
        DeliveryAddress updatedAddress = addressService.updateDeliveryAddress(id, deliveryAddress);
        if(updatedAddress != null) {
            return HttpResult.success(updatedAddress);
        } else {
            return HttpResult.failure(ResultCodeEnum.NOT_FOUND);
        }
    }

    @DeleteMapping("/addresses/{id}")
    @Operation(summary = "删除配送地址", description = "删除指定ID的配送地址")
    public HttpResult<Void> deleteDeliveryAddress(@PathVariable Long id) {
        boolean deleted = addressService.deleteDeliveryAddress(id);
        if(deleted) {
            return HttpResult.success();
        } else {
            return HttpResult.failure(ResultCodeEnum.NOT_FOUND);
        }
    }
}
